/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Manager_Image_Utils;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import javax.swing.ImageIcon;

/**
 *
 * @author Admin
 */
public class Library {

    public String[] EXTENSIONS = new String[]{
        "jpg", "JPG", "png", "bmp","tiff","gif" // and other formats you need
    };
    public  boolean  CheckExtensions(String name)
    {
        for(String item:EXTENSIONS)
        {
            if(item.equals(name))
                return true;
        }
        return false;
    }
    public boolean CheckImagesFlder(String path) {
        File dir = null;
        try {
            dir = new File(path);
            if (dir.isDirectory()) {
                for (File f : dir.listFiles()) {
                    if (f.isDirectory()) {
                        continue;
                    } else {
                        for (String extentsion : EXTENSIONS) {
                            boolean temp = f.getName().endsWith("." + extentsion);
                            if (temp) {
                                return (true);
                            }
                        }
                    }
                }
            }

        } catch (Exception e) {
        }
        return false;
    }

    public static boolean isImage(String image_path) {
        Image image = new ImageIcon(image_path).getImage();
        if (image.getWidth(null) == -1) {
            return false;
        } else {
            return true;
        }
    }

    public static BufferedImage resizeImageWithHint(BufferedImage originalImage, int W, int H, int type) {
        BufferedImage resizedImage = new BufferedImage(W, H, type);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(originalImage, 0, 0, W, H, null);
        g.dispose();
        g.setComposite(AlphaComposite.Src);
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.setRenderingHint(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        return resizedImage;
    }

    public static BufferedImage scaleImage(int WIDTH, int HEIGHT, ImageIcon ii) {
        BufferedImage bi = null;
        try {
            bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.SCALE_SMOOTH);
            Graphics2D g2d = (Graphics2D) bi.createGraphics();
            g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
            g2d.drawImage(ii.getImage(), 0, 0, WIDTH, HEIGHT, null);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return bi;
    }

    public static BufferedImage getScaledImage(BufferedImage image, int width, int height) throws IOException {
        int imageWidth = image.getWidth();
        int imageHeight = image.getHeight();

        double scaleX = (double) width / imageWidth;
        double scaleY = (double) height / imageHeight;
        AffineTransform scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
        AffineTransformOp bilinearScaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR);
        return bilinearScaleOp.filter(
                image,
                new BufferedImage(width, height, image.getType()));
    }

    public static BufferedImage Rotate(BufferedImage old, int Width, int Height, int angle) {
        BufferedImage newimg = new BufferedImage(old.getHeight(), old.getWidth(), old.getType());
        Graphics2D grap = (Graphics2D) newimg.getGraphics();
        AffineTransform identity = new AffineTransform();
        AffineTransform trans = new AffineTransform();
        trans.setTransform(identity);
        grap.rotate(Math.toRadians(angle), newimg.getWidth() * 1.0 / 2, newimg.getHeight() * 1.0 / 2);
        trans.rotate(Math.toRadians(angle), newimg.getWidth() * 1.0 / 2, newimg.getHeight() * 1.0 / 2);

        grap.drawImage(old, 0, 0, old.getWidth(), old.getHeight(), null);
        return newimg;

    }

    public static BufferedImage Resize(BufferedImage old, int Width, int Height, float zoom) {
        Width = (int) Math.ceil(zoom * Width);
        Height = (int) Math.ceil(zoom * Height);
        BufferedImage newimg = new BufferedImage(Width, Height, old.getType());
        Graphics2D grap = (Graphics2D) newimg.getGraphics();
        grap.drawImage(old, 0, 0, Width, Height, null);
        return newimg;
    }

    public static void CopyFile(File sourceFile, File destFile) throws IOException {
        if (!sourceFile.exists()) {
            return;
        }
        if (!destFile.exists()) {
            destFile.createNewFile();
        }
        FileChannel source = null;
        FileChannel destination = null;
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(destFile).getChannel();
        if (destination != null && source != null) {
            destination.transferFrom(source, 0, source.size());
        }
        if (source != null) {
            source.close();
        }
        if (destination != null) {
            destination.close();
        }
    }

    public static void CutFile(File sourceFile, File destFile) {
        

        if (!destFile.exists()) {
            sourceFile.renameTo(destFile);
        }
    }
    public static boolean DeleteFile(File _file)
    {
        try {
            _file.delete();
            return true;
        } catch (Exception e) {
             e.printStackTrace();
        }
        return false;
    }

    public static void print(final Image img) {
        PrinterJob printjob = PrinterJob.getPrinterJob();
        printjob.setJobName("Print");

        ImgPrinter printable = new ImgPrinter(img);

        try {
            System.out.println("Printing.");
            printable.printPage();
        } catch (PrinterException ex) {
            System.out.println("NO PAGE FOUND." + ex);
        }
    }

   
}
